const app = getApp()
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		resdata: {},
		name: '',
		card_id: '',
		phone: '',
		email: '',
		bank_item: '',
		bank_card_id: '',
		maskFlag: false,
		bankfilter: '',
		imageUrl:
			'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1mCvO4.img?w=534&h=600&m=6&x=174&y=291&s=293&d=293',
		imageUrl2: '',
		banklist0: [
			{
				name: '阿拉尔银行',
				id: 1
			},
			{
				name: '阿拉尔津汇村镇银行',
				id: 2
			},
			{
				name: 'sdfs镇银行',
				id: 3
			}
		],
		banklist: [
			{
				name: '阿拉尔银行',
				id: 1
			},
			{
				name: '阿拉尔津汇村镇银行',
				id: 2
			},
			{
				name: 'sdfs镇银行',
				id: 3
			}
		],
		abc: '',
		id_card_opposite: '', // 头像
		id_card_straight: '', // 国徽
		province: '',
		city: '',
		area: '',
		province_code: '',
		city_code: '',
		area_code: '',
		address: '',
		customItem: '',
		region: [],
		isClicking: false // 防重复点击
	},
	observers: {
		name: function (aaa) {
			console.log(aaa)
		}
	},
	filters(res) {
		// const arr = this.data.banklist0
		// const after = [...arr].filter(item => item.bank_name.includes(res.detail.value))
		// this.setData({
		//   banklist: after
		// })
		this.getbank(res.detail.value)
	},

	checkone(ev) {
		const set1 = ev.currentTarget.dataset
		this.setData({
			bank_item: set1.item,
			maskFlag: false
		})
	},
	sumbit() {
		const name = this.data.name
		const card_id = this.data.card_id
		const phone = this.data.phone
		const email = this.data.email
		const province = this.data.province
		const city = this.data.city
		const area = this.data.area
		const province_code = this.data.province_code
		const city_code = this.data.city_code
		const area_code = this.data.area_code
		const address = this.data.address
		const bank_card_id = this.data.bank_card_id
		const bank_code = this.data.bank_item.bank_code
		const id_card_opposite = this.data.id_card_opposite
		const id_card_straight = this.data.id_card_straight
		const flag =
			!name ||
			!card_id ||
			!phone ||
			!email ||
			!bank_card_id ||
			!bank_code ||
			!id_card_opposite ||
			!id_card_straight ||
			!province ||
			!city ||
			!area ||
			!address
		console.log(flag, 666)
		if (flag) {
			wx.showToast({
				title: '请填写完整！',
				icon: 'error'
			})
			return
		}
		if (this.data.isClicking) {
			return
		}
		this.setData({
			isClicking: true
		})
		wx.showLoading({
			// 开启loading动画
			title: '提交中'
		})
		wx.request({
			url: app.siteInfo.siteroot_web + '?c=salesman&a=applyAccount', // 对于post请求，单独拼接参数
			cachetime: '0',
			method: 'POST',
			data: {
				c: 'salesman',
				a: 'applyAccount',
				user_id: app.globalData.userId,
				type: app.globalData.userType,
				name,
				card_id,
				phone,
				email,
				province,
				city,
				area,
				province_code,
				city_code,
				area_code,
				address,
				bank_card_id,
				bank_code,
				id_card_opposite,
				id_card_straight
			},
			success: resp => {
				const res = resp.data
				if (res.code == 0) {
					// 请求成功
					wx.hideLoading()
					this.setData({
						isClicking: false
					})
					wx.showToast({
						title: '提交成功！'
					})
					setTimeout(() => {
						wx.switchTab({
							url: '/zh_jdgjb/pages/logs/logs'
						})
					}, 2000)
				} else {
					// 请求失败
					wx.showToast({
						icon: 'error',
						title: res.msg
					})
				}
			}
		})
	},

	expendmask() {
		this.setData({
			maskFlag: true
		})
	},

	hide(ev) {
		this.setData({
			maskFlag: ev.target.id == 'mask1' ? false : true
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.init()
	},

	init() {
		this.getbank('')
		// 用于回显信息
		wx.request({
			url: app.siteInfo.siteroot_web,
			cachetime: '0',
			method: 'GET',
			data: {
				c: 'salesman',
				a: 'getAccount',
				user_id: app.globalData.userId,
				type: app.globalData.userType
			},
			success: resp => {
				const res = resp.data
				if (res.code == 0) {
					// 请求成功
					this.setData({
						name: res.data.name,
						card_id: res.data.card_id,
						phone: res.data.phone,
						email: res.data.email,
						province: res.data.province,
						city: res.data.city,
						area: res.data.area,
						province_code: res.data.province_code,
						city_code: res.data.city_code,
						area_code: res.data.area_code,
						region: res.data.province
							? [res.data.province, res.data.city, res.data.area]
							: [],
						address: res.data.address,
						bank_item: res.data.card_id,
						bank_card_id: res.data.bank_card_id,
						id_card_opposite: res.data.id_card_opposite,
						id_card_straight: res.data.id_card_straight,
						bank_item: {
							bank_code: res.data.bank_code,
							bank_name: res.data.bank_name
						}
					})
				} else {
					// 请求失败
					wx.showToast({
						icon: 'none',
						title: res.msg
					})
				}
			}
		})
	},

	getbank(name) {
		wx.request({
			url: app.siteInfo.siteroot_web,
			cachetime: '0',
			method: 'GET',
			data: {
				c: 'salesman', // 路径参数，去掉do
				a: 'getBank',
				bank_code: '',
				bank_name: name
			},
			success: resp => {
				const res = resp.data
				if (res.code == 0) {
					this.setData({
						banklist0: res.data.data,
						banklist: res.data.data
					})
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {},
	chooseImage() {
		wx.chooseImage({
			count: 1, // 可选择的图片数量
			sizeType: ['compressed'], // 压缩图片
			sourceType: ['album', 'camera'], // 来源：相册或相机
			success: res => {
				console.log('图片--tempFiles', res)
				// 将选择的图片上传到服务器
				// this.setData({
				//   abc: res.tempFilePaths[0] // 本地图片可以展示到image src
				// })
				this.uploadImage(res.tempFilePaths[0], 1)
			}
		})
	},
	chooseImage2() {
		wx.chooseImage({
			count: 1, // 可选择的图片数量
			sizeType: ['compressed'], // 压缩图片
			sourceType: ['album', 'camera'], // 来源：相册或相机
			success: res => {
				console.log('图片--tempFiles', res)
				// 将选择的图片上传到服务器
				// this.setData({
				//   abc: res.tempFilePaths[0] // 本地图片可以展示到image src
				// })
				this.uploadImage(res.tempFilePaths[0], 2)
			}
		})
	},
	uploadImage(imagePath, flag) {
		wx.uploadFile({
			// 上传到业务服务器
			url: app.siteInfo.siteroot_web + '?c=index&a=fileUpload', // 上传图片的接口地址
			filePath: imagePath, // 图片文件路径
			name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
			// header: {
			//   "content-type": "multipart/form-data", // 前端file对象上传
			// },
			dataType: 'JSON',
			responseType: 'json',
			success: res => {
				// 上传成功后，将服务器返回的图片地址更新到image标签中
				// console.log('oss地址', JSON.parse(res.data))
				const json2 = JSON.parse(res.data)
				if (flag == 1) {
					this.setData({
						id_card_opposite: json2.data
					})
					this.invoke(json2.data)
				} else {
					this.setData({
						id_card_straight: json2.data
					})
				}
			},
			fail: function (res) {
				console.log('res--上传失败', res)
			}
		})
	},
	invoke(url) {
		console.log('url--oss地址', url)
		wx.serviceMarket
			.invokeService({
				service: 'wx79ac3de8be320b71', // '固定为服务商OCR的appid，非小程序appid',
				api: 'OcrAllInOne',
				data: {
					// oss地址可以识别出来
					img_url: url,
					data_type: 3,
					ocr_type: 1
				}
			})
			.then(res => {
				console.log('invokeService success', res)
				this.setData({
					name: res.data.idcard_res.name.text,
					card_id: res.data.idcard_res.id.text
				})
				// wx.showModal({
				//   title: 'cost',
				//   content: JSON.stringify(res),
				// })
			})
			.catch(err => {
				console.error('invokeService fail', err)
				wx.showModal({
					title: 'fail',
					content: '未识别到身份信息，请重新上传'
				})
			})
	},
	checkphone() {
		const phone = this.data.phone
		const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
		if (phone.includes('*')) {
			if (phone.length != 11) {
				wx.showToast({
					title: '手机号格式不对',
					icon: 'error'
				})
			}
		} else {
			if (!reg.test(phone)) {
				wx.showToast({
					title: '手机号格式不对',
					icon: 'error'
				})
			}
		}
	},
	checkemail() {
		console.log(45)
		const email = this.data.email
		const reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
		if (!reg.test(email)) {
			wx.showToast({
				title: '邮箱地址格式不对',
				icon: 'error'
			})
		}
	},
	bindRegionChange: function (e) {
		console.log('picker发送选择改变，携带值为中文', e.detail.value)
		console.log('picker发送选择改变，携带值为code', e.detail.code)
		this.setData({
			region: e.detail.value,
			province: e.detail.value[0],
			province_code: e.detail.code[0],
			city: e.detail.value[1],
			city_code: e.detail.code[1],
			area: e.detail.value[2],
			area_code: e.detail.code[2]
		})
	}
})
